package indi.zhifa.core.common.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AnnotationTypeFilter;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class PackageScanUtil {
    public static <T extends Annotation> List<Class<?>> scanBasePackage(List<String> pPaths, Class<T> pClass){
        ClassPathScanningCandidateComponentProvider scanner = new MyClassPathScanner();
        scanner.addIncludeFilter(new AnnotationTypeFilter(pClass));
        scanner.addIncludeFilter((metadataReader, metadataReaderFactory) -> true);
        List<Class<?>> classes = new ArrayList<>();
        for(String path : pPaths){
            scanner.findCandidateComponents(path).forEach(beanDefinition -> {
                try {
                    // 加载类
                    Class<?> clazz = Class.forName(beanDefinition.getBeanClassName());
                    classes.add(clazz);
                } catch (ClassNotFoundException e) {
                    log.error("扫描"+path+"时发生错误, ");
                    log.error(CommonUtil.getMessage(e));
                }
            });
        }
        return classes;
    }
}
